import pandas as pd

pd.read_excel("data_09/data/neg.xls", header=None).head()
pd.read_excel("data_09/data/pos.xls", header=None).head()
pd.read_csv("data_09/comments.csv").head()

import jieba
import numpy as np

# 加载语料库文件，并导入数据
neg = pd.read_excel('data_09/data/neg.xls', header=None)
pos = pd.read_excel('data_09/data/pos.xls', header=None)


# jieba 分词


def word_cut(x): return jieba.lcut(x)


# 出现错误AttributeError: 'float' object has no attribute 'decode'，以下两行代码是添加代码
pos.dropna(inplace=True)
neg.dropna(inplace=True)

pos['words'] = pos[0].apply(word_cut)
neg['words'] = neg[0].apply(word_cut)

# 使用 1 表示积极情绪，0 表示消极情绪，并完成数组拼接
x = np.concatenate((pos['words'], neg['words']))
y = np.concatenate((np.ones(len(pos)), np.zeros(len(neg))))

# 将 Ndarray 保存为二进制文件备用
np.save('X_train.npy', x)
np.save('y_train.npy', y)

print('done.')
np.load('X_train.npy', allow_pickle=True)

# Word2Vec 处理
from gensim.models.word2vec import Word2Vec
import warnings

warnings.filterwarnings('ignore')  # 忽略警告

# 导入上面保存的分词数组
X_train = np.load('X_train.npy', allow_pickle=True)

# 训练 Word2Vec 浅层神经网络模型
w2v = Word2Vec(vector_size=300, min_count=10)
w2v.build_vocab(X_train)
w2v.train(X_train, total_examples=w2v.corpus_count, epochs=w2v.epochs)


# def sum_vec(text):
#     # 对每个句子的词向量进行求和计算
#     vec = np.zeros(300).reshape((1, 300))
#     for word in text:
#         try:
#             vec += w2v[word].reshape((1, 300))
#         except KeyError:
#             continue
#     return vec

#修改后
def sum_vec(text):
    vec = np.zeros(300)
    for word in text:
       try:
           vec += w2v.wv[word]
       except KeyError:
            continue
    return vec


# 将词向量保存为 Ndarray
train_vec = np.concatenate([sum_vec(z) for z in X_train])
# 保存 Word2Vec 模型及词向量
w2v.save('w2v_model.pkl')
np.save('X_train_vec.npy', train_vec)
print('done.')

#训练情绪分类模型，决策树方法
from sklearn.externals import joblib
from sklearn.tree import DecisionTreeClassifier

# 导入词向量为训练特征
X = np.load('X_train_vec.npy')
# 导入情绪分类作为目标特征
y = np.load('y_train.npy')
# 构建支持向量机分类模型
model = DecisionTreeClassifier()
# 训练模型
model.fit(X, y)
# 保存模型为二进制文件
joblib.dump(model, 'dt_model.pkl')



#对用户评判进行情绪判断
# 读取 Word2Vec 并对新输入进行词向量计算
def sum_vec(words):
    # 读取 Word2Vec 模型
    w2v = Word2Vec.load('w2v_model.pkl')
    vec = np.zeros(300).reshape((1, 300))
    for word in words:
        try:
            vec += w2v[word].reshape((1, 300))
        except KeyError:
            continue
    return vec
#进行情绪判断
# 读取蓝桥云课评论
df = pd.read_csv("data_09/comments.csv", header=0)
comment_sentiment = []
for string in df['评论内容']:
    # 对评论分词
    words = jieba.lcut(str(string))
    words_vec = sum_vec(words)
    # 读取支持向量机模型
    model = joblib.load('dt_model.pkl')
    result = model.predict(words_vec)
    comment_sentiment.append(result[0])

    # 实时返回积极或消极结果
    if int(result[0]) == 1:
        print(string, '[积极]')
    else:
        print(string, '[消极]')

# 将情绪结果合并到原数据文件中
merged = pd.concat([df, pd.Series(comment_sentiment, name='用户情绪')], axis=1)
pd.DataFrame.to_csv(merged, 'comment_sentiment.csv')  # 储存文件以备后用